package com.bigtotoro.shuiyincamera.image;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bigtotoro.shuiyincamera.BaseActivity;
import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.adapter.CommonAdapter;
import com.bigtotoro.shuiyincamera.adapter.ViewHolder;
import com.bigtotoro.shuiyincamera.model.ColorEntity;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * 设置文字
 */
public class SettingTextActivity extends BaseActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    private EditText edit_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_text);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.font_0));

        initBase();
        initView();
        initGrid();
        initData();
        initBar();

    }

    private int  mAalpha = 100;
    int seekBarLeft;
    SeekBar alphaSeekBar;
    TextView alphaToolTip;
    private void initBar() {
        alphaSeekBar = (SeekBar) findViewById(R.id.opacitySeekBar);
        alphaToolTip = findViewById(R.id.opacityToolTip);
        alphaSeekBar.setOnSeekBarChangeListener(this);
        seekBarLeft = alphaSeekBar.getPaddingLeft();
        alphaSeekBar.setProgress(mAalpha); //设置默认透明度
    }


    private void initData() {  //可选的颜色列表
        mColorList.add(new ColorEntity(1,"FF3399"));
        mColorList.add(new ColorEntity(2,"CC33CC"));
        mColorList.add(new ColorEntity(3,"990066"));
        mColorList.add(new ColorEntity(4,"006699"));
        mColorList.add(new ColorEntity(5,"FFCC00"));
        mColorList.add(new ColorEntity(6,"66FF66"));
        mColorList.add(new ColorEntity(7,"999966"));
        mColorList.add(new ColorEntity(8,"00CCFF"));
        mColorList.add(new ColorEntity(9,"888888"));
        mColorList.add(new ColorEntity(10,"000000"));
        commonAdapter.notifyDataSetChanged();
    }


    private List<ColorEntity> mColorList = new ArrayList<>();
    private List<ColorEntity> mSelectData = new ArrayList<>();
    private GridView grid;
    private CommonAdapter<ColorEntity> commonAdapter;
    private void initGrid() {
        grid = findViewById(R.id.grid);
        commonAdapter = new CommonAdapter<ColorEntity>(this,
                mColorList,
                R.layout.item_grid02) {
            @Override
            public void convert(ViewHolder holder, ColorEntity colorEntity) {
                    //选中改变
                    for (ColorEntity obj:mSelectData){
                        if (obj.getId() == colorEntity.getId()){
                            holder.getConvertView().findViewById(R.id.item).setSelected(true);
                        }else {
                            holder.getConvertView().findViewById(R.id.item).setSelected(false);
                        }
                    }
                    View content_item = holder.getConvertView().findViewById(R.id.content_item);
                    content_item.setBackgroundColor(Color.parseColor("#"+colorEntity.getColor_number()));
                    View item = holder.getConvertView().findViewById(R.id.item);

                    item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = (int) v.getTag();
                            ColorEntity colorEntity = mColorList.get(position);
                            //点击边框变黑
                            mSelectData.clear();
                            mSelectData.add(colorEntity);
                            commonAdapter.notifyDataSetChanged();
                        }
                    });
                    item.setTag(holder.getPosition());
            }
        };
        grid.setAdapter(commonAdapter);
    }

    private void initView() {
        findViewById(R.id.btn_submit).setOnClickListener(this);
//        findViewById(R.id.btn_cancel).setOnClickListener(this);
        edit_content = findViewById(R.id.edit_content);
    }

    private String content,color;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_cancel:
                content = edit_content.getText().toString().trim();
//                if (content ==null || content.length() ==0){
//                    Toast.makeText(this, "不能输入空内容", Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Intent intent = new Intent();

//                intent.putExtra("content"," ");
//                intent.putExtra("color","FF3399");
//                intent.putExtra("alpha","255");
//                setResult(RESULT_OK,intent);
                finish();
                break;
            case R.id.btn_submit:
                content = edit_content.getText().toString().trim();
                if (content ==null || content.length() ==0){
                    Toast.makeText(this, "不能输入空内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mSelectData!=null && mSelectData.size()>0){
                    color = mSelectData.get(0).getColor_number();
                }else {
                    color = "FF3399";
                }

                Intent intent1 = new Intent();
                intent1.putExtra("content",content);
                intent1.putExtra("color",color);
                intent1.putExtra("alpha",mAalpha);
                setResult(RESULT_OK,intent1);
                finish();
                break;
        }
    }
    Rect thumbRect;
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar.getId() == R.id.opacitySeekBar) {

            mAalpha = progress;
            thumbRect = seekBar.getThumb().getBounds();
            alphaToolTip.setX(seekBarLeft + thumbRect.left - 30); //透明度显示文字左移20

            if (progress < 10)
                alphaToolTip.setText("  " + mAalpha +"%");
            else if (progress < 100)
                alphaToolTip.setText(" " + mAalpha +"%");
            else
                alphaToolTip.setText(mAalpha + "" +"%");
        }

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        thumbRect = alphaSeekBar.getThumb().getBounds();

        alphaToolTip.setX(seekBarLeft + thumbRect.left - 30);
        if (mAalpha < 10)
            alphaToolTip.setText("  " + mAalpha +"%");
        else if (mAalpha < 100)
            alphaToolTip.setText(" " + mAalpha +"%");
        else
            alphaToolTip.setText(mAalpha + "" +"%");

        thumbRect = alphaSeekBar.getThumb().getBounds();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SettingTextActivity");//统计页面
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SettingTextActivity");//统计页面
        MobclickAgent.onPause(this);
    }
}
